home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / DOCZ16.ZIP;1 / DOCZ.LIF / STRMCPY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-24  |  2.8 KB  |  97 lines

  1. #ifdef DOCUMENTATION
  2.  ******************************* DOCZ Header *********************************
  3. .MODULE                strmcpy
  4. .LIBRARY             csub
  5. .TYPE                 function
  6. .APPLICATION        string
  7. .SYSTEM                msdos
  8. .SYSTEM                vms
  9. .SYSTEM                unix
  10. .AUTHOR                Software Toolz
  11. .DESCRIPTION
  12.     Perform a bounded string copy and produce a NULL terminated string
  13. .ARGUMENTS
  14.     char *strmcpy(dest,source,maxchar)
  15.         char    *dest,                    /* (w) destination string */
  16.                 *source;                 /* (r) source string */
  17.         int    maxchar;                 /* (r) maximum characters to copy */
  18. .NARRATIVE
  19.     Strmcpy behaves exactly like strncpy() except that the destination string
  20.     is always NULL-terminated.  The "maxchar" argument specifies the size of
  21.     the destination array (NOT including the NULL).  If the length of the source
  22.     string exceeds "maxchar" then the last character copied to the destination
  23.     string will be a NULL character.
  24. .RETURNS
  25.     The address of the destination string.
  26. .COMMENTS
  27.     This function eliminates a common C pitfall caused by strncpy() producing
  28.     un-terminated strings.
  29. .LANGUAGE
  30.     VMS, MSDOS:  Assembly; UNIX:    C
  31. .FIXES                4/15/88
  32.     Changed documentation to match performance of function.
  33.     Used to be:  The "maxchar" argument specifies the size of the destination
  34.         array (including the NULL).
  35.     Is now:    The "maxchar" argument specifies the size of the destination
  36.         array (NOT including the NULL).
  37. .ENDOC                END DOCUMENTATION
  38.  *****************************************************************************
  39. #endif
  40.  
  41. #include <stdio.h>
  42. #include <ctype.h>
  43. #include "csub.h"
  44.  
  45. /*****************************************************************************
  46.     Strmcpy
  47. *****************************************************************************/
  48. char *strmcpy(dest,source,maxchar)
  49.     char    *dest,                    /* (w) destination string */
  50.             *source;                 /* (r) source string */
  51.     int    maxchar;                 /* (r) maximum characters to copy */
  52. {
  53.     register i = 0;
  54.  
  55.     while ((source[i]) && (i < maxchar))
  56.     {
  57.         dest[i] = source[i];
  58.         ++i;
  59.     }
  60.  
  61.     dest[i] = CHAR_NUL;
  62.  
  63.     return (dest);
  64. }    /* end strmcpy */
  65.  
  66.  
  67. #ifdef TESTING                 /* test program */
  68.  
  69. /*****************************************************************************
  70.     C test program
  71. *****************************************************************************/
  72.  
  73. main(argc,argv)
  74.     int    argc;
  75.     char    *argv[];
  76. {
  77.     char    buff[128];
  78.  
  79.     if (argc == 3)
  80.     {
  81.         strmcpy(buff,argv[1],atoi(argv[2]));
  82.         printf("input string length = %d\r\n",strlen(argv[1]));
  83.         printf("input string = (%s)\r\n",argv[1]);
  84.         printf("maximum characters = %d\r\n",atoi(argv[2]));
  85.         printf("result = (%s)\r\n",buff);
  86.     }
  87.     else
  88.         puts("STRMCPY [string] [max. characters]");
  89. }
  90.  
  91. #endif                            /* test program */
  92.  
  93. /*****************************************************************************
  94.     End STRMCPY.C
  95. *****************************************************************************/
  96.  
  97.